import { action, observable, toJS } from 'mobx';
import * as Api from '../apis/receiveDoc';
import { WeaTools, WeaLocaleProvider } from 'ecCom';
import { WeaForm } from 'comsMobx';
import { message } from 'antd';

const { getLabel } = WeaLocaleProvider;

class RegistDocStore {
  getDatas = () => { };
  @observable loading = false;
  @observable handworkForm = new WeaForm();
  @observable workflowData = [];
  @observable tabs = []
  @observable isShowAllTab = false;
  @observable registDailogKey = '0';
  @observable handworkForm = new WeaForm();// 手工登记form
  @observable handwordCondition = [];
  @observable handworkShow = false;
  @observable selectRowId = [];
  @observable registDailogKey = '0';

  @observable eleRegistShow = false;

  @observable registProcessShow = false; // 登记处理
  @observable fileId = {} // 文件id
  @observable fileInfoArr = {
    Document_Attachments: [],
    Document_Text: [],
  } // 文件信息

  @observable docId = '';
  @observable step = '1';
  @observable type = '1'; // type  0 代表手工登记的二级弹窗 1 智能登记二级弹窗 2 扫码登记

  // 创建流程
  @observable createWorkflowDialog = false;
  @observable tabs = [];
  @observable workflowData = [];
  @observable workflowKeys = '0'

  // 登记处理
  @observable receiveSmartShow = false;

  @observable isReceivecheck = false;
  @observable document_identifier = '';
  @observable isShowAllTab = false;
  isReally = false;


  @action.bound
  setMobxState(params = {}) {
    Object.keys(params).forEach((v) => {
      this[v] = params[v];
    });
  }

  // 点击来文登记按钮
  @action.bound
  showRegistDialog() {
    this.loading = true;
    Api.getRegistrationForm().then((data) => {
      this.loading = false;
      this.setRegistrationForm(data);
      this.setMobxState({ eleRegistShow: !this.eleRegistShow, type: '1' });
      if (data.condition[0]) {
        this.isShowAllTab = data.condition[0].isShowAllTab != false;
        this.registDailogKey = (!this.isShowAllTab && (this.registDailogKey == '0' || this.registDailogKey == '1')) ? '2' : '0';
        this.type = (!this.isShowAllTab && this.registDailogKey == '2') ? '2' : '1';
      }
    });
  }

  // 处理手工登记form表单
  @action.bound setRegistrationForm(data) {
    this.handworkForm = new WeaForm();
    let file = [];
    const items = data.condition[0].items;
    for (let i = 0; i < data.condition[0].items.length; i++) {
      const item = items[i];
      if (item.conditionType.toUpperCase() == WeaTools.types.UPLOAD) {
        this.fileInfoArr[item.domkey[0]] = item.datas || [];
      }
      // 为了美观 把附件放在最后
      if (item.domkey[0] == 'Document_Attachments') {
        items.splice(i, 1);
        file.push(item);
        i--;
      }
      // 为了美观 把正文放在最后
      if (item.domkey[0] == 'Document_Text') {
        items.splice(i, 1);
        file.unshift(item);
        i--;
      }
    }
    data.condition[0].items = [...data.condition[0].items, ...file];
    this.handworkForm.initFormFields(toJS(data.condition));
    this.handwordCondition = data.condition;
  }

  // 登记保存
  @action.bound
  registSave(CreatFlow = false) {
    let formData = this.handworkForm.getFormParams();
    if (this.fileId.Document_Text) {
      formData.Document_Attachments = this.fileId.Document_Attachments;
      formData.Document_Text = this.fileId.Document_Text;
    }
    if (formData.Document_Title == '' || formData.Document_Text == '') {
      message.warning(getLabel('', '你有必填项没有填'));
      return;
    }
    const params = {
      ...formData,
      receiveDocType: this.registDailogKey,
      receiveStatus: '0',
      documentIdentifier: this.document_identifier,
    };
    this.loading = true;
    Api.saveRegistrationFormData(params).then((data) => {
      this.loading = false;
      if (data.api_status) {
        this.getDatas({}, false, false);
        if (CreatFlow == true) {
          if (data.message) {
            this.selectRowId = [data.message];
          }
          this.document_identifier = data.documentIdentifier;
          this.createWorkflowDialog = true;
        } else {
          this.eleRegistShowOnCancel();
        }
      } else {
        message.warning(data.message);
      }
    });
  }

  // 收文登记
  @action.bound
  delList(id) {
    const params = {
      ids: id,
    };
    Api.delList(params).then((data) => {
      if (data.api_status) {
        message.success(getLabel('', '删除成功'));
        this.getDatas({}, false, false);
      }
    });
  }

  @action.bound
  getBaseInfoForDemo() {
    Api.getBaseInfoForDemo().then((data) => {
      this.tabs = data.groupinfo || [];
      this.getWorkflowListByTypeForDemo();
    });
  }

  @action.bound
  getWorkflowListByTypeForDemo() {
    let params = {};
    toJS(this.tabs).map((item) => {
      if (item.viewcondition == this.workflowKeys) {
        params.groupid = item.groupid;
        params.title = item.title;
      }
    });
    Api.getWorkflowListByTypeForDemo(params).then((data) => {
      if (data.api_status) {
        this.workflowData = data.data;
      }
    });
  }

  // 智能登记 下一步
  @action.bound
  smartRegistration() {
    const imageFileNameArr = toJS(this.fileInfoArr.Document_Text).map(item => item.filename);
    const imageFileName = imageFileNameArr.join(',');
    this.loading = true;
    const params = {
      docType: this.registDailogKey,
      imageFileId: this.fileId.Document_Text,
      imageFileName,
      attachmentIds: this.fileId.Document_Attachments,
      isReally: this.isReally ? '0' : '1', // 返回的数据 真 或 假 1假 0真
    };
    Api.smartRegistration(params).then((data) => {
      this.loading = false;
      if (data.api_status) {
        this.docId = data.imagefileid;
        this.setRegistrationForm(data);
        this.step = '2';
      } else {
        this.dialogOnCancel();
        message.error(getLabel('', '文件转换失败'));
      }
    });
  }

  @action.bound
  changeTab(v) {
    this.registDailogKey = v;
    this.clearFile();
    if (v == '0' || v == '1') {
      this.type = '1';
    } else if (v == '2') {
      this.type = '2';
      this.getHandForm();
    } else if (v == '3') {
      this.type = '0';
      this.getHandForm();
    }
  }

  // 点击查看
  @action.bound
  getRegistrationFormData() {
    const params = {
      id: this.selectRowId.join(','),
    };
    Api.getRegistrationFormData(params).then((data) => {
      if (data.api_status) {
        this.setRegistrationForm(data);
        this.docId = data.message;
        this.eleRegistShow = true;
        this.step = '2';
        this.type = '0';
      }
    });
  }

  // 电子登记关闭
  @action.bound
  eleRegistShowOnCancel() {
    this.eleRegistShow = false;
    this.step = '1';
    this.registDailogKey = '0';
    this.type = '1';
    this.clearFile();
    this.document_identifier = '';
    this.selectRowId = [];
    this.isReceivecheck = false;
  }

  // 清除文件信息
  @action.bound
  clearFile() {
    this.fileId = {};
    this.fileInfoArr = {
      Document_Attachments: [],
      Document_Text: [],
    };
  }

  @action.bound
  getHandForm() {
    Api.getRegistrationForm().then((data) => {
      this.setRegistrationForm(data);
    });
  }

  // 智能登记的时候显示上传文件信息
  @action.bound
  setFileInfo(type, ids, list, name) {
    this.fileId[type] = ids.join(',');
    this.fileInfoArr[type] = list;
    if (name == 'smartRegist') {
      let nameArr = [];
      list.map((item) => {
        nameArr.push(item.filename);
      });
    }
    if (this.registDailogKey == '3') {
      if (toJS(this.fileInfoArr.Document_Text)[0] && toJS(this.fileInfoArr.Document_Text)[0].fileExtendName != 'pdf') {
        this.getChangeWordToPdf();
      }
    }
  }

  @action.bound
  changeWorkflowTab(v) {
    this.workflowKeys = v;
    this.getWorkflowListByTypeForDemo();
  }

  // 新建流程
  @action.bound openCreatFlowWindow(workflowid) {
    if (this.createWorkflowLoading) {
      return;
    }
    this.createWorkflowLoading = true;
    const params = {
      ids: this.selectRowId.join(','),
      createWfid: workflowid,
      isInnerChange: this.isInnerChange,
    };

    Api.getRequstId(params).then((data) => {
      this.createWorkflowLoading = false;
      if (data.status) {
        this.getDatas({}, false, false);
        let formData = this.handworkForm.getFormParams();
        formData.Document_Attachments = this.fileId.Document_Attachments;
        formData.Document_Text = this.fileId.Document_Text;
        this.eleRegistShowOnCancel();
        this.createWorkflowOnCancel();
        window.open(data.url);
        this.showCreatFlow = false;
      } else {
        message.error(data.message);
      }
    });
  }

  @action.bound
  createWorkflowOnCancel(cancel = false) {
    this.createWorkflowDialog = false;
    this.document_identifier = '';
    this.selectRowId = [];
    if (!cancel) {
      this.step = '1';
      this.eleRegistShowOnCancel();
    }
  }

  // 登记处理
  @action.bound
  nextTept() {
    this.handworkForm.validate().then((f) => {
      if (f.isValid) {
        this.type = '0';
        this.step = '2';
        if (this.fileInfoArr.Document_Text[0].fileExtendName != 'pdf') {
          this.getChangeWordToPdf();
        }
      } else {
        f.showErrors();
      }
    });
  }

  @action.bound
  previousTept() {
    this.step = '1';
    this.clearFile();
  }

  @action.bound
  getChangeWordToPdf() {
    const params = {
      imageFileId: this.fileId.Document_Text,
    };
    Api.changeWordToPdf(params).then((data) => {
      if (data.api_status) {
        this.docId = data.message;
      } else {
        message.error(data.message);
      }
    });
  }
}


export default RegistDocStore;
